IsA Operator

Used to determine the class of a particular object reference.


Syntax

result=object IsA object class

PartTypeDescription
result Boolean Any container expecting a Boolean value.
object Object Any object name.
object class Object class Any object class or class interface.


Notes

If object is of type object class, the IsA operator returns True; if not, it returns False. If Object is Nil, then IsA always returns False.

For example, in the following code, IsA returns False:

Dim t as TCPSocket
If t IsA TCPSocket then  //returns False
//do something here
End if

You must instantiate the local variable t using the New operator so that it is non- Nil

Dim t as New TCPSocket
If t IsA TCPSocket then  //returns True
//do something here
End if

:

The IsA operator returns True for the object's own class as well as the super class from which that class was derived, all the way to the Object class. IsA will report that all classes ultimately derive from Object.

For example, suppose you create a subclass of EditField called SecureEditField, that disables the Cut and Copy commands in the Edit menu. If EditField1 is an instance of SecureEditField, the tests:

EditField1 IsA EditField

and

EditField1 IsA SecureEditField

both return True.

You use the IsA operator to cast objects. With the IsA operator, you test whether an object is of a specific subclass and, if it is, cast it as that type to do something specific with it.

Here is an example that uses a For loop to cycle through all the controls in a window to test whether each control is an EditField. If it is, it casts the control, gets its name and the value of its Text property, and assigns the contents of the EditField to the field in a database table named fieldname.

Dim r as DatabaseRecord
Dim fieldname, fieldContents as String
.
.
For i = 0 to Self.ControlCount-1  //number of controls in window
  If Self.control(i) IsA EditField then
     fieldname = EditField(control(i)).DataField //cast it
  //the text property assigned to the contents of that field
     fieldContents = EditField(control(i)).text
  r.column(fieldname)= fieldContents
  end if
next

As you can see, the code is generic since it uses no references to specific EditFields or databases.

A second example of casting uses the Window function to get the frontmost window and then using IsA to determine which what kind of window it is (i.e., Window1, Window2) and then casts it as that type to access something specific about the window

If Window(0) IsA Window1 then
  Window1(Window(0)).Pushbutton1.enabled = True
End if

You also use the IsA operator in connection with a class interface. If a custom class implements a particular class interface, then IsA return True. For example, if you create a class interface, myClassInterface, and custom control classes subclassed from the StaticText, EditField, and Canvas classes all implement myClassInterface, then the IsA operator will return True for each such test.

In this way, you can write generic code that examines all controls in a window to determine whether each control implements a particular class interface. If it does, then you can execute any methods of the class interface.

For example, if you wrote a class interface called FontInterfaceManger that updates the font displayed by a control using a method called UpdateTheFont, you use code such as this to call the method only for the controls in a window that implement the FontInterfaceManager class interface. This following example, which can be the Action event handler of a PushButton in a window, examines all the controls in the window and calls the UpdateTheFont method for those controls that implement the class interface FontInterfaceManager.

Dim i as Integer
For i=0 to Self.ControlCount-1
 If Self.Control(i) IsA FontInterfaceManager then
  FontInterfaceManager( Self.Control(i)).UpdateTheFont
 End If
Next

Example

This example uses the IsA operator to determine if the variable p is a PushButton object. If it is, the caption property is assigned "OK".

If p IsA PushButton Then
 PushButton(p).Caption="OK"
End if